src/views/auth/Login.vue
<template>
  <div class="login-container">
    <div class="login-form">
      <h2>用户登录</h2>
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="80px">
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="formData.email" placeholder="请输入邮箱" type="email" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="formData.password" placeholder="请输入密码" type="password" show-password />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin" :loading="authStore.loading" class="w-full">登录</el-button>
        </el-form-item>
        <el-link type="primary" :underline="false" @click="$router.push('/register')" class="block text-center">
          没有账号？立即注册
        </el-link>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { useAuthStore } from '../../stores/auth';

const router = useRouter();
const authStore = useAuthStore();
const formRef = ref();

const formData = ref({
  email: '',
  password: ''
});

const rules = {
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入有效的邮箱地址', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少为6位', trigger: 'blur' }
  ]
};

const handleLogin = async () => {
  try {
    await formRef.value.validate();
    await authStore.login(formData.value.email, formData.value.password);
    ElMessage.success('登录成功');
    
    // 获取保存的重定向地址，如果没有则默认跳转到首页
    const redirectPath = localStorage.getItem('redirectAfterLogin') || '/';
    localStorage.removeItem('redirectAfterLogin');
    router.push(redirectPath);
  } catch (error: any) {
    ElMessage.error(error.response?.data?.message || '登录失败，请重试');
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
}

.login-form {
  width: 400px;
  padding: 2rem;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-form h2 {
  text-align: center;
  margin-bottom: 1.5rem;
}
</style>